﻿IF EXISTS (SELECT * FROM information_schema.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME = 'OTMaterialSave')
	BEGIN
		DROP  Procedure  OTMaterialSave
	END
GO


CREATE PROCEDURE OTMaterialSave 	
	(			
		@OTEntregaId AS INT,
		@empresaId AS INT,
		@fecha AS DATETIME,		
		@articuloId AS VARCHAR(13) = NULL,
		@tipo AS CHAR(1) = NULL,
		@cantidad AS DECIMAL(10,2)  = NULL,
		@operarioId AS INT = NULL,
		@departamento AS VARCHAR(100)  = NULL
	)	
AS
BEGIN	
	SET NOCOUNT ON	
	
	DECLARE @CostoArticuloUnitario AS MONEY
	DECLARE @CostoArticuloTotal AS MONEY
	
	SELECT @CostoArticuloUnitario = art_costo_actual, @CostoArticuloTotal = art_costo_actual * @cantidad
	FROM Articulo
	WHERE Emp_Id = @empresaId
	AND	  Art_id = @articuloId

		
	INSERT INTO Lit_OTMaterial
		(OTEntregaId, Fecha, ArticuloId, Tipo, Cantidad, OperarioId, Departamento, CostoArticuloUnitario, CostoArticuloTotal)
	VALUES 
		(@OTEntregaId, @fecha, @articuloId, @tipo, @cantidad, @operarioId, @departamento, @CostoArticuloUnitario, @CostoArticuloTotal)
	
	SET NOCOUNT OFF
END
GO   